package com.elsa.DataStatistics.statistics;

import com.elsa.DataStatistics.mapper.*;
import com.elsa.DataStatistics.pojo.DayMenu;
import com.elsa.DataStatistics.pojo.DayMp;
import com.elsa.DataStatistics.pojo.Menu;
import com.elsa.DataStatistics.pojo.MenuOrder;
import com.elsa.DataStatistics.utils.CalculateUtil;
import com.elsa.DataStatistics.utils.TimeUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@Slf4j
public class DayMPStatistics {

    @Autowired
    private TimeUtil timeUtil;

    @Autowired
    private DayMapper dayMapper;

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private DayMpMapper dayMpMapper;

    @Autowired
    private CalculateUtil calculateUtil;

    @Autowired
    private MenuMapper menuMapper;

    @Autowired
    private MenuOrderMapper menuOrderMapper;

    public void dayMPStatistics(String oId) {

        int dId = timeUtil.getDayTime();

        List<MenuOrder> menuOrderList = menuOrderMapper.selectMenuOrderMapperFromOrderDataStatistics(oId);

        for (int i = 0; i < menuOrderList.size(); i++) {
            DayMp dayMp = dayMpMapper.selectDayMpByDIdAndMIdAndCId(dId, menuOrderList.get(i).getCId(), menuOrderList.get(i).getMId());
            if(dayMp == null) {
                dayMp = new DayMp();
                dayMp.setDmpId(dayMpMapper.selectCount(null) + 1);
                dayMp.setDpId(dId);
                dayMp.setMId(menuOrderList.get(i).getMId());
                dayMp.setMName(menuOrderList.get(i).getMName());
                dayMp.setCId(menuOrderList.get(i).getCId());
                Menu menu = menuMapper.selectById(menuOrderList.get(i).getMId());
                dayMp.setDmpCalorie(menu.getMCalorie() * menuOrderList.get(i).getMoNum());
                dayMp.setDmpNum(menuOrderList.get(i).getMoNum());
                log.info(String.valueOf(dayMp));
                dayMpMapper.insert(dayMp);
            } else {
                dayMp.setCId(menuOrderList.get(i).getCId());
                Menu menu = menuMapper.selectById(menuOrderList.get(i).getMId());
                dayMp.setDmpCalorie(dayMp.getDmpCalorie() + menu.getMCalorie() * menuOrderList.get(i).getMoNum());
                dayMp.setDmpNum(dayMp.getDmpNum() + menuOrderList.get(i).getMoNum());
                log.info(String.valueOf(dayMp));
                dayMpMapper.updateById(dayMp);
            }
        }
    }

}
